<?php
require_once('bin/recaptchalib.php');
require_once("bin/data.php");
require_once ("bin/_variables.php");
require_once ("oauth-php/tw.php");

$privatekey = "6LdQ2s0SAAAAAEN7QID9Hu2r9-N3oPpia1miXxnU";
$publickey = "6LdQ2s0SAAAAAFpBvSC6i1a2QilTbMQt5I5JW7Lq";

$captchahtml = recaptcha_get_html($publickey);

$oauth_token = getOAuthToken();

$title = "Join";
$link="";
$header="";
$footer="";
$content = <<<EOF
<div id="formentry">
<h2>Register</h2>
<form action="join.php" method="post">
<table width="100%" border="0">
<tr>
	<td align="right">
		<span>Username:</span>
	</td>
	<td>
		<input type="text" name="i1" id="i1" value=""/>
	</td>
</tr>
<tr>
	<td align="right">
		<span>Email Address:</span>
	</td>
	<td>
		<input type="text" name="i2" id="i2" value=""/>
	</td>
</tr>
<tr>
	<td align="right">
		<span>Password:</span>
	</td>
	<td>
		<input type="password" name="i3" id="i3" value=""/>
	</td>
</tr>
<tr>
	<td align="right">
		<span>Re-enter Password:</span>
	</td>
	<td>
		<input type="password" name="i4" id="i4" value=""/>
	</td>
</tr>
<tr>
	<td></td>
	<td>
		<span>Security check:</span> {$captchahtml}
	</td>
</tr>
<tr>
	<td></td>
	<td>
		<input type="submit" name="r" value="Register" />
	</td>
</tr>
</table>
</form>
</div>
<div id="formentry">
Or sign in using one of these services:
<br />

<a href="https://oauth.twitter.com/2/authorize?oauth_mode=flow_web_client&oauth_client_identifier=DZ36yHdU1GPQTJpxFBbTfg"><img src="images/social_signin_folio.png" /></a>
</div>
EOF;

if (isset($_REQUEST['c']))
{
	global $tblusers;
	
	$confirmationcode = mysql_real_escape_string($_REQUEST['c']);
	// I need to look up the user by code, then confirm them.
	$sql = "UPDATE `$tblusers` SET enabled = 1 where confirmationcode = '$confirmationcode';";
	$result = mysql_query($sql) or die(mysql_error() );
	if ($result > 0)
	{
		$content = <<<EOF
		<div id="singlelinereply">Thank you for confirming your account. To sign in, please <a href="signin.php">click here</a>.</div>
EOF;
	}
}

if (isset($_POST['r']))
{
	$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

	if (!$resp->is_valid)
	{
		die ("The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")");
	}
	else
	{
		$confirmationcode = register_user(@$_POST['i1'], @$_POST['i2'], @$_POST['i3'], @$_POST['i4']);
		if (isset($confirmationcode))
		{
			email_user(@$_POST['i1'], @$_POST['i2'], $confirmationcode); 
			header("Location:confirm.php");
		}
		else
		{
			// the passwords do not match.
			$content = <<<EOF
			<div id="singlelinereply">The 2 passwords entered did not match. Please press your browser's 'back' button and try again.</div>
EOF;
		}
	}
}
function register_user($name, $email, $password, $repassword)
{
	global $tblusers;
	
	//need to create a random confirmation code
	$confirmationcode = substr(md5(uniqid(rand(), true)), 0, 16);
	$name=mysql_real_escape_string($name);
	$email=mysql_real_escape_string($email);
	$password=mysql_real_escape_string($password);
	$repassword=mysql_real_escape_string($repassword);
	
	if ($password!=$repassword)
	{
		return null;
	}
	$sql = "INSERT INTO `$tblusers` VALUES
		(
		NULL,
		'$confirmationcode',
		'0',
		'$email',
		'$name',
		'$password',
		'$first',
		'$last',
		'$countryid',
		'$desc',
		'$website',
		'1',
		NOW(),
		CURRENT_TIMESTAMP);";

	$result = mysql_query($sql) or die(mysql_error() );

	return $confirmationcode;	
}

function email_user($uname, $email, $confirmationcode)
{
	global $mailFrom;
	global $siteName;
	
	$headers = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
	$headers .= 'From: '.$mailFrom."\r\n";
	$message = 'Greetings '.$uname.',

Thank you for opening an account with '.$siteName.'.com!

Your username is: '.$uname.'

To confirm your new account, please click here: http://www.'.$siteName.'.com/join.php?c='.$confirmationcode;

	@mail($email, "$siteName.com Registration", $message, $headers);
	print $message;
}




include ("base.php");
echo $html;

?>
